﻿using AutoMapper;
using ESSManageService.IServices.IBusinessServices;
using ESSManageService.IServices.IDBServices;
using ESSManageService.Model.DBModels.Info;
using ESSManageService.Model.ViewModels;
using ESSManageService.Model;
using ESSManageService.Services.BusinessServices;
using ESSManageService.Services.DBServices;
using Microsoft.AspNetCore.Mvc;
using ESSManageService.Enum.ViewEnum;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Microsoft.AspNetCore.Authorization;

namespace ESSManageService.API.Controllers
{
    /// <summary>
    /// 业务控制器，提供与业务相关的 API 接口
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [Produces("application/json")]
    //[ApiExplorerSettings(IgnoreApi = false)]//控制器是否显示在Swagger中
    [Authorize] // 整个控制器需要认证
    public class BusinessController : BaseApiController
    {
        private readonly IMapper _mapper; 
        readonly ClientWebSocketService _clientWebSocketService;
        public BusinessController(IMapper mapper, ClientWebSocketService clientWebSocketService)
        {
            _clientWebSocketService = clientWebSocketService;
            _mapper = mapper;
        }
        /// <summary>
        /// 培训状态命令
        /// </summary>
        /// <returns></returns>

        [HttpPost]
        [Authorize(Roles = "管理员,教练员")]
        public MessageModel<string> PostTrainingState()
        {
            var data = new MessageModel<string>();
            try
            {
                if (_clientWebSocketService.SendTrainingState(1))
                {
                    data.status = (int)ResponseType.Success;
                    data.msg = "发送培训状态命令成功";
                }
                else
                {
                    data.status = (int)ResponseType.Warning;
                    data.msg = "发送培训状态命令失败，与模拟服务链接中断";
                }
            }
            catch (Exception ex)
            {
                data.status = (int)ResponseType.UnhandledException;
                data.msg = $"{ex.Message}";
            }
            return data;
        }

    }
}
